package com.markerhub.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.Blog;
import com.markerhub.entity.Course;
import com.markerhub.entity.ManagerActivity;
import com.markerhub.entity.ManagerTeacher;
import com.markerhub.service.BlogService;
import com.markerhub.service.ManagerActivityService;
import com.markerhub.service.ManagerTeacherService;
import com.markerhub.util.ShiroUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/manager-activity")
public class ManagerActivityController {
    @Autowired
    ManagerActivityService managerActivityService;
    @Autowired
    ManagerTeacherService managerTeacherService;


    @RequiresAuthentication
    @GetMapping("/activitys")
    public Result list(@RequestParam(defaultValue = "1") Integer currentPage) {

        Page page = new Page(currentPage, 5);
        IPage pageData = managerActivityService.page(page, new QueryWrapper<ManagerActivity>().orderByDesc("created_time"));

        return Result.succ(pageData);
    }

    @RequiresAuthentication
    @GetMapping("/activity/{id}")
    public Result detail(@PathVariable(name = "id") Long id) {
        ManagerActivity activity = managerActivityService.getById(id);
        Assert.notNull(activity, "该活动已被删除");

        return Result.succ(activity);
    }


    @RequiresAuthentication
    @PostMapping("/activity/edit")
    public Result edit(@Validated @RequestBody ManagerActivity activity) {
        managerActivityService.saveOrUpdate(activity);
        return Result.succ(null);
    }


    @RequiresAuthentication
    @GetMapping("/activity-by-name")
    public Result getActivityByName(@RequestParam("name") String name) {
        ManagerActivity activity = managerActivityService.getByActivityName(name);  // 根据活动名称查询活动信息
        if (activity == null) {
            return Result.fail("活动未找到");
        }
        return Result.succ(activity);  // 返回活动对象，包括 ID
    }


    @GetMapping("/all")
    public Result getAllActivities() {
        List<ManagerActivity> activities = managerActivityService.list(); // 获取所有活动
        return Result.succ(activities);
    }


    @RequiresAuthentication
    @PutMapping("/activity/{id}/publish")
    public Result publishActivity(@PathVariable("id") Long activityId) {
        ManagerActivity activity = managerActivityService.getById(activityId);
        if (activity == null) {
            return Result.succ("活动未找到");
        }
        // 更新活动状态为已发布
        activity.setStatus(1); // 1: 已发布
        managerActivityService.saveOrUpdate(activity);
        return Result.succ("活动已发布");
    }


    @RequiresAuthentication
    @PutMapping("/activity/{id}/remove")
    public Result removeActivity(@PathVariable("id") Long activityId) {
        ManagerActivity activity = managerActivityService.getById(activityId);
        if (activity == null) {
            return Result.succ("活动未找到");
        }

        activity.setStatus(3); // 1: 已发布
        managerActivityService.saveOrUpdate(activity);
        return Result.succ("活动已删除");
    }


    @RequiresAuthentication
    @PutMapping("/activity/{id}/close")
    public Result closeActivity(@PathVariable("id") Long activityId) {
        ManagerActivity activity = managerActivityService.getById(activityId);
        if (activity == null) {
            return Result.succ("活动未找到");
        }

        activity.setStatus(2); // 1: 已发布
        managerActivityService.saveOrUpdate(activity);
        return Result.succ("活动已关闭");
    }


    @RequiresAuthentication
    @GetMapping("/manager-activity/select")
    public Result getActivities(
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
            @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {

        IPage activities = managerActivityService.getActivities(title, status, currentPage, pageSize);
        return Result.succ(activities);
    }
  /*  @GetMapping("/blog/{id}")
    public Result detail(@PathVariable(name = "id") Long id) {
        Blog blog = blogService.getById(id);
        Assert.notNull(blog, "该博客已被删除");

        return Result.succ(blog);
    }

    @RequiresAuthentication
    @PostMapping("/blog/edit")
    public Result edit(@Validated @RequestBody Blog blog) {

//        Assert.isTrue(false, "公开版不能任意编辑！");

        Blog temp = null;
        if(blog.getId() != null) {
            temp = blogService.getById(blog.getId());
            // 只能编辑自己的文章
            System.out.println(ShiroUtil.getProfile().getId());
            Assert.isTrue(temp.getUserId().longValue() == ShiroUtil.getProfile().getId().longValue(), "没有权限编辑");

        } else {

            temp = new Blog();
            temp.setUserId(ShiroUtil.getProfile().getId());
            temp.setCreated(LocalDateTime.now());
            temp.setStatus(0);
        }

        BeanUtil.copyProperties(blog, temp, "id", "userId", "created", "status");
        blogService.saveOrUpdate(temp);

        return Result.succ(null);
    }*/
}
//        Assert.isTrue(false, "公开版不能任意编辑！");
     /*   ManagerActivity activity1=managerActivityService.getById(activity.getId());
        ManagerTeacher teacher = managerTeacherService.getByTeachername(activity1.getCreator());
        log.info(String.valueOf(teacher));
        Long teacherLoginId = teacher.getLoginId();

        ManagerActivity temp = null;
        if(activity.getId() != null) {
            temp = managerActivityService.getById(activity.getId());
            // 只能编辑自己的文章
            System.out.println(ShiroUtil.getProfile().getId());
            Assert.isTrue(teacherLoginId.longValue() == ShiroUtil.getProfile().getId().longValue(), "没有权限编辑");

        } else {

            temp = new ManagerActivity();
            temp.setStatus(1);
        }

        BeanUtil.copyProperties(activity, temp, "id","status","creator");
        managerActivityService.saveOrUpdate(temp);*/